import { OnInit, Component, Input, ViewChild } from '@angular/core';
import { TeacherDTO } from 'src/app/project/model/icm/icm-system-user/teacher/teacher.dto';
import { ScoreEntryDetailComponent } from './score-entry-detail.component';
import { QueryOptions } from 'src/app/project/model/common/query-options';
import { StudentCourseGradeDTO } from 'src/app/project/model/icm/icm-student-academic/score-entry/student-course-grade.dto';
import { ObjectUtil } from 'src/app/project/utils/object-util';
import { TABLE_PARAMETER } from '../../setting/table-parameter';
import { STColumn } from '@delon/abc';
import { StudentScoreService } from 'src/app/project/service/icm/icm-student-academic/score-entry/student-score.service';
import { NzMessageService } from 'ng-zorro-antd';
import { TeacherSerivce } from 'src/app/project/service/icm/icm-system-user/teacher/teacher.service';
import { SettingsService } from '@delon/theme';
import { format } from 'date-fns';

@Component({
  selector: 'app-score-entry-list',
  templateUrl: './score-entry-list.component.html',
  styles: [],
})
export class ScoreEntryListComponent implements OnInit {
  constructor(
    private studentCourseGradeServie: StudentScoreService,
    private msg: NzMessageService,
    private teacherService: TeacherSerivce,
    private settingService: SettingsService,
  ) {}

  @ViewChild('scoreEntryDetailComponent', { static: false })
  scoreEntryDetailComponent: ScoreEntryDetailComponent;

  /**
   * 分页参数初始化
   */
  queryOptions: QueryOptions = {
    page: 0,
    size: 20,
    sort: 'year,desc,isCorrect,asc',
  };

  /**
   * 查询参数
   */
  @Input()
  queryParams;

  /**
   * 当前登录教师
   */
  teacher: TeacherDTO;
  teacherId = null;

  /**
   * 请求数据标识
   */
  loading = false;

  /**
   * 表格数据
   */
  tableData: Array<StudentCourseGradeDTO> = [];

  /**
   * 列表参数
   */
  TABLE_PARAMETER = ObjectUtil.deepClone(TABLE_PARAMETER);

  /**
   * 表格的列定义
   */
  stColumns: STColumn[] = [
    {
      title: '序号',
      render: 'number',
      width: '2%',
      className: 'text-center',
    },
    {
      title: '学院名称',
      index: 'academyName',
      width: '5%',
      className: 'text-left',
    },
    {
      title: '行政班级',
      index: 'className',
      width: '5%',
      className: 'text-left',
    },
    {
      title: '学生学号',
      index: 'studentCode',
      width: '4%',
      className: 'text-left',
    },
    {
      title: '学生姓名',
      index: 'studentName',
      width: '3%',
      className: 'text-left',
    },
    {
      title: '课程编号',
      index: 'courseCode',
      width: '4%',
      className: 'text-left',
    },
    {
      title: '课程名称',
      index: 'courseName',
      width: '4%',
      className: 'text-left',
    },
    {
      title: '年度',
      index: 'year',
      width: '4%',
      className: 'text-center',
      format(item) {
        return `${item.year} - ${Number.parseInt(item.year, 10) + 1} 学年`;
      },
    },
    {
      title: '学期',
      index: 'termType',
      width: '3%',
      className: 'text-left',
      format(item) {
        return item.termType === 'LAST_TERM' ? '第一学期' : '第二学期';
      },
    },
    {
      title: '是否批改',
      index: 'isCorrect',
      width: '3%',
      className: 'text-center',
      format(item) {
        return item.isCorrect ? '已批改' : '未批改';
      },
    },
    {
      title: '操作',
      render: 'operations',
      index: 'operation',
      width: '4%',
      className: 'text-center',
      fixed: 'right',
    },
  ];

  /**
   * 表格变化事件，用于双击、排序，翻页等操作
   * @param e event
   */
  change(e: any): void {
    // 双击事件
    if (e.type === 'dblClick') {
      this.loading = true;
      // tslint:disable-next-line: deprecation
      this.studentCourseGradeServie.findById(e.dblClick.item.id).subscribe(
        data => {
          this.scoreEntryDetailComponent.edit(true, data);
        },
        null,
        () => {
          this.loading = false;
        },
      );
    }
    // 排序事件
    if (e.type === 'sort') {
      if (e.sort.map) {
        const temp = e.sort.map.sort;
        if (temp) {
          this.queryOptions.sort = temp.replace(/[\.-]/g, ',');
        }
      }
      this.loadData();
    }
    // 翻页设置
    if (e.type === 'ps' || e.type === 'pi') {
      this.queryOptions.page = e.pi - 1;
      this.queryOptions.size = e.ps;
      this.loadData();
    }
  }

  /**
   * 获取学生课程成绩数据
   */
  loadData() {
    this.loading = true;
    this.studentCourseGradeServie
      .findOnePage(
        this.queryOptions,
        this.queryParams.academyName,
        this.queryParams.className,
        this.queryParams.studentName,
        this.queryParams.courseName,
        this.teacherId,
        // null,
        this.queryParams.year !== null ? format(this.queryParams.year, 'YYYY') : '',
        this.queryParams.termType,
        this.queryParams.isCorrect,
      )
      // tslint:disable-next-line: deprecation
      .subscribe(
        data => {
          if (data) {
            this.tableData = data.data;
            this.TABLE_PARAMETER.page.total = data.totalRecords;
          }
        },
        null,
        () => {
          this.loading = false;
        },
      );
  }

  /**
   * 添加教师
   */
  create() {
    this.scoreEntryDetailComponent.edit(false, null);
  }

  /**
   * 批改学生课程成绩
   * @param row 学生课程成绩
   */
  onUpdate(row: StudentCourseGradeDTO) {
    this.loading = true;
    // tslint:disable-next-line: deprecation
    this.studentCourseGradeServie.findById(row.id).subscribe(
      data => {
        this.scoreEntryDetailComponent.edit(false, data);
      },
      null,
      () => {
        this.loading = false;
      },
    );
  }

  /**
   * 查看
   */
  view(row: StudentCourseGradeDTO) {
    // tslint:disable-next-line: deprecation
    this.studentCourseGradeServie.findById(row.id).subscribe(
      data => {
        this.scoreEntryDetailComponent.edit(true, data);
      },
      null,
      () => {
        this.loading = false;
      },
    );
  }

  /**
   * 查看
   */
  modify(row: StudentCourseGradeDTO) {
    // tslint:disable-next-line: deprecation
    this.studentCourseGradeServie.findById(row.id).subscribe(
      data => {
        this.scoreEntryDetailComponent.edit(false, data);
      },
      null,
      () => {
        this.loading = false;
      },
    );
  }

  ngOnInit() {
    this.teacherId = this.settingService.user.id;
    this.teacherService.findById(this.teacherId).subscribe(data => {
      this.teacher = data;
    });
    this.loadData();
  }
}
